package com.magicode.common.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.entiy.base.NewsBuyHistory;
import com.magicode.cms.entiy.base.Operate;
import com.magicode.cms.entiy.base.User;
import com.magicode.cms.entiy.base.VipBuyHistory;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.common.service.IPermissionService;
import com.magicode.common.util.DateUtil;

@Service("permissionService")
public class PermissionServiceImpl implements IPermissionService {

	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
	
	@Override
	public List<Operate> getFLFuncPerListByUser(User user) {
		String hql = " select ro.operate from com.magicode.cms.entiy.base.RolesOperates ro, "
				+ " com.magicode.cms.entiy.base.User u where ro.role in elements(u.roles) and u.id='"+user.getId()+"' ";
		hql += " and ro.operateType=1 and ro.operate.noPermission=0 ";
		List<Operate> list = this.commonDaoService.getListByHQL(hql);
//		Map map = new HashMap();
//		for (Operate op : list) {
//			map.put(op.getFullUrl(), op.)
//		}
//		List<String> list = new ArrayList<String>();
//		list.add("article");
//		list.add("channel");
//		list.add("comment");
//		list.add("dictionarytype");
//		list.add("dictionary");
//		list.add("tag");
//		list.add("video");
//		list.add("videoprogram");
//		list.add("video");
//		list.add("user");
//		list.add("source");
//		list.add("role");
//		list.add("offline");
//		list.add("province");
//		list.add("city");
//		list.add("cmsHelp");
//		list.add("advertise");
//		// TODO 待完善
		return list;
	}

	@Override
	public List<Operate> getFLFuncPerListBySession(HttpSession session) {
		User user = (User) session.getAttribute("user");
		String hql = " select ro.operate from com.magicode.cms.entiy.base.RolesOperates ro, "
				+ " com.magicode.cms.entiy.base.User u where ro.role in elements(u.roles) and u.id='"+user.getId()+"' ";
		hql += " and ro.operateType=1 and ro.operate.noPermission=0 "
				+ " and ro.operate.source='class' "
				+ " order by ro.operate.orderCode ";
		List<Operate> list = this.commonDaoService.getListByHQL(hql);
		return list;
	}

	@Override
	public Boolean validUserPermission(String url, User user) {
		String hql = "select ro.operate from com.magicode.cms.entiy.base.RolesOperates ro,com.magicode.cms.entiy.base.User u where ro.operate.fullUrl='"+url+"' ";
		hql += " and ro.role in elements(u.roles) and u.id='"+user.getId()+"' and ro.operateType=1 and ro.operate.noPermission=0 ";
		List list = this.commonDaoService.getListByHQL(hql);
		if(list.size() > 0){
			return true;
		}
		return false;
	}

	@Override
	public Map<String, String> getNotValidMap() {
		Map map = new HashMap<String, String>();
		String hql = "select o.fullUrl from com.magicode.cms.entiy.base.Operate o where o.noPermission=1 ";
		List<String> list = this.commonDaoService.getListByHQL(hql);
		for (String string : list) {
			map.put(string, string);
		}
		return map;
	}

	@Override
	public List<Operate> getPermissionButtonList(
			List<JsonOperation> jsonOperations, String url) {
		// TODO 待完善
		return null;
	}

	

}
